export const setcache = function() {
  const run = function(): void {
    for (let i in Game.rooms) {
      let room = Game.rooms[i];
      let costMatrix: number[] | undefined = undefined;
      if (!room.memory || room.memory.costMatrix == undefined) {
        costMatrix = roomCallBack(room).serialize();
      } else if (Game.time % 10 == 0) {
        costMatrix = roomCallBack(room).serialize();
      }
      if (costMatrix != undefined) {
        if (room.controller && room.controller.my) {
          room.memory.costMatrix = costMatrix;
        }
        room.costMatrix = PathFinder.CostMatrix.deserialize(costMatrix);
        continue;
      }
      room.costMatrix = PathFinder.CostMatrix.
        deserialize(room.memory.costMatrix);
    }
    return;
  };
  const roomCallBack = function (room: Room): CostMatrix {
    let costs = new PathFinder.CostMatrix;
    room.find(FIND_STRUCTURES).forEach(struct => {
      if (struct.structureType === STRUCTURE_ROAD) {
        // road 成本设置为1
        costs .set(struct.pos.x, struct.pos.y, 1);
      } else if (struct.structureType !== STRUCTURE_CONTAINER
          && (struct.structureType !== STRUCTURE_RAMPART || !struct.my)) {
        // 除 container 和 rampart 外所有建筑或不是自己的建筑成本设置为255
        costs.set(struct.pos .x, struct.pos.y, 255)
      }
    });
    room.find(FIND_MY_CONSTRUCTION_SITES).forEach(cons => {
      if (cons.structureType !== 'road' 
          && cons.structureType !=='rampart' 
          && cons.structureType !== "container") {
        costs.set(cons.pos.x, cons.pos.y, 255);
      }
    });
    room.find(FIND_HOSTILE_CREEPS).forEach(creep => {
      costs.set(creep.pos.x, creep.pos.y, 255);
    });
    return costs;
  };
  run();
}